package com.example.mymvvmdemo.models.main;


import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.databinding.ActivityMainBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.models.main.fragment.device.DeviceFragment;
import com.example.mymvvmdemo.models.main.fragment.home.HomeFragment;
import com.example.mymvvmdemo.models.main.fragment.mine.MineFragment;
import com.example.mymvvmdemo.models.main.fragment.notification.NotificationFragment;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

public class MainActivity extends BaseLoadActivity<ActivityMainBinding> implements View.OnClickListener {

    public static void startMainActivity(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }

    private MainViewModel viewModel;
    private HomeFragment homeFragment;
    private DeviceFragment deviceFragment;
    private NotificationFragment notificationFragment;
    private MineFragment mineFragment;

    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;


    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(MainViewModel.class);
        initFragment();
    }

    @Override
    protected void initListener() {
        binding.llHome.setOnClickListener(this);
        binding.llDevice.setOnClickListener(this);
        binding.llNotification.setOnClickListener(this);
        binding.llMine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                changFragment(homeFragment);
                break;
            case R.id.ll_device:
                changFragment(deviceFragment);
                break;
            case R.id.ll_notification:
                changFragment(notificationFragment);
                break;
            case R.id.ll_mine:
                changFragment(mineFragment);
                break;
            default:
                break;
        }
    }

    /*********************************************自定义方法******************************************/
    public void initFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();

        homeFragment = (HomeFragment) fragmentManager.findFragmentByTag("homeFragment");
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, homeFragment, "homeFragment").commit();
        }

        deviceFragment = (DeviceFragment) fragmentManager.findFragmentByTag("deviceFragment");
        if (deviceFragment == null) {
            deviceFragment = new DeviceFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, deviceFragment, "deviceFragment").commit();
        }

        notificationFragment = (NotificationFragment) fragmentManager.findFragmentByTag("notificationFragment");
        if (notificationFragment == null) {
            notificationFragment = new NotificationFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, notificationFragment, "notificationFragment").commit();
        }

        mineFragment = (MineFragment) fragmentManager.findFragmentByTag("mineFragment");
        if (mineFragment == null) {
            mineFragment = new MineFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, mineFragment, "mineFragment").commit();
        }

        homeFragment.setMainViewModel(viewModel);
        deviceFragment.setMainViewModel(viewModel);
        notificationFragment.setMainViewModel(viewModel);
        mineFragment.setMainViewModel(viewModel);

        //默认选中设备fragment
        changFragment(homeFragment);
    }

    public void changFragment(Fragment fragment) {
        try {
            if (homeFragment == fragment) {
                getSupportFragmentManager().beginTransaction().show(homeFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(deviceFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(notificationFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(mineFragment).commit();

                binding.tvHome.setTextColor(getResources().getColor(R.color.green_one));
                binding.tvDevice.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvNotification.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvMine.setTextColor(getResources().getColor(R.color.gray_one));

                binding.ivHome.setImageResource(R.drawable.ic_svg_main_home_select);
                binding.ivDevice.setImageResource(R.drawable.ic_svg_main_device_no_select);
                binding.ivNotification.setImageResource(R.drawable.ic_svg_main_notification_no_select);
                binding.ivMine.setImageResource(R.drawable.ic_svg_main_mine_no_select);

                binding.clAll.setBackgroundResource(R.drawable.bg_gradient_green);
//                binding.clAll.setBackgroundResource(R.mipmap.ic_home_back);
            } else if (deviceFragment == fragment) {
                getSupportFragmentManager().beginTransaction().hide(homeFragment).commit();
                getSupportFragmentManager().beginTransaction().show(deviceFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(notificationFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(mineFragment).commit();

                binding.ivHome.setImageResource(R.drawable.ic_svg_main_home_no_select);
                binding.ivDevice.setImageResource(R.drawable.ic_svg_main_device_select);
                binding.ivNotification.setImageResource(R.drawable.ic_svg_main_notification_no_select);
                binding.ivMine.setImageResource(R.drawable.ic_svg_main_mine_no_select);

                binding.tvHome.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvDevice.setTextColor(getResources().getColor(R.color.green_one));
                binding.tvNotification.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvMine.setTextColor(getResources().getColor(R.color.gray_one));

                binding.clAll.setBackgroundColor(getResources().getColor(R.color.gray_four));
//                binding.clAll.setBackgroundResource(R.mipmap.ic_notification_back);
            } else if (notificationFragment == fragment) {
                getSupportFragmentManager().beginTransaction().hide(homeFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(deviceFragment).commit();
                getSupportFragmentManager().beginTransaction().show(notificationFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(mineFragment).commit();

                binding.ivHome.setImageResource(R.drawable.ic_svg_main_home_no_select);
                binding.ivDevice.setImageResource(R.drawable.ic_svg_main_device_no_select);
                binding.ivNotification.setImageResource(R.drawable.ic_svg_main_notification_select);
                binding.ivMine.setImageResource(R.drawable.ic_svg_main_mine_no_select);

                binding.tvHome.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvDevice.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvNotification.setTextColor(getResources().getColor(R.color.green_one));
                binding.tvMine.setTextColor(getResources().getColor(R.color.gray_one));

                binding.clAll.setBackgroundColor(getResources().getColor(R.color.gray_four));
//                binding.clAll.setBackgroundResource(R.mipmap.ic_notification_back);
            } else if (mineFragment == fragment) {
                getSupportFragmentManager().beginTransaction().hide(homeFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(deviceFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(notificationFragment).commit();
                getSupportFragmentManager().beginTransaction().show(mineFragment).commit();

                binding.ivHome.setImageResource(R.drawable.ic_svg_main_home_no_select);
                binding.ivNotification.setImageResource(R.drawable.ic_svg_main_device_no_select);
                binding.ivNotification.setImageResource(R.drawable.ic_svg_main_notification_no_select);
                binding.ivMine.setImageResource(R.drawable.ic_svg_main_mine_select);

                binding.tvHome.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvDevice.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvNotification.setTextColor(getResources().getColor(R.color.gray_one));
                binding.tvMine.setTextColor(getResources().getColor(R.color.green_one));

                binding.clAll.setBackgroundColor(getResources().getColor(R.color.gray_four));
//                binding.clAll.setBackgroundResource(R.mipmap.ic_my_back);
            }

        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }
    }

    /*********************************************生命周期******************************************/
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //拦截用户返回按键
            //判断用户是否处于放大二维码的状态，如果是则缩小二维码，否则退出界面
            try {
                //先判断是否处于轮询
                if (System.currentTimeMillis() - viewModel.lastCurrent > 2000) { //如果上次点击距离本次超过两秒，则重新开始计算
                    viewModel.lastCurrent = System.currentTimeMillis();
                    toast(R.string.app_exit, ToastUtils.TOA_W);
                    viewModel.num = 1;
                } else {
                    viewModel.num++;
                }
                if (viewModel.num >= 2) {
                    finish();
                }
            } catch (Exception e) {
                LogUtils.e(e.getMessage());
            }
        }
        return false;
    }

}